#include <stdio.h>

// 为什么结构体会采用 fread / fwrite 来读写
// 1. 类型不统一
// 2. 可以将二进制转化为文本使其统一，降低效率，占用多余的储存空间，
typedef struct student
{
    int num;
    char name[30];
    char sex;
    float math;
    float english;
    float chinese;
}Stu;

int main()
{
    FILE * pfw = fopen("stu.data","r+");
    if(pfw == NULL)
    {
        exit(-1);
    }

//    Stu s;
//    while(fread((void*)&s,sizeof(Stu),1,pfw))
//    {
//        printf("num     = %d\n",s.num);
//        printf("name    = %s\n",s.name);
//        printf("sex     = %c\n",s.sex);
//        printf("math    = %.2f\n",s.math);
//        printf("english = %.2f\n",s.english);
//        printf("chinese = %.2f\n",s.chinese);
//        printf("---------------------------\n");
//    }

    Stu s[3];
    int n;
    while((n =fread((void*)s,sizeof(Stu),3,pfw)) > 0)
    {
        for(int i=0;i <n; i++)
        {
            printf("num     = %d\n",s[i].num);
            printf("name    = %s\n",s[i].name);
            printf("sex     = %c\n",s[i].sex);
            printf("math    = %.2f\n",s[i].math);
            printf("english = %.2f\n",s[i].english);
            printf("chinese = %.2f\n",s[i].chinese);
            printf("---------------------------\n");
        }
    }
    return 0;
}
int main1()
{
    Stu s[5]=
    {
        {1001,"wk",'x',99,99,99},
        {1002,"sj",'x',39,78,100},
        {1003,"by",'x',79,56,100},
        {1004,"ldh",'y',69,100,56},
        {1005,"zs",'z',23,12,58}
    };
    FILE * pfs = fopen("stu.data","w+");
    if(pfs == NULL)
    {
        exit(-1);
    }
    for( int i =0; i <5; i++)
    {
        fwrite((void*)&s[i],sizeof (Stu),1,pfs);
    }
    fclose(pfs);
//    printf("Hello World!\n");
    return 0;
}
